/*
 * Copyright(C) 2019 FUYUN DATA SERVICES CO.,LTD. - All Rights Reserved
 * Unauthorized copying of this file, via any medium is strictly prohibited
 * Proprietary and confidential
 * 该源代码版权归属福韵数据服务有限公司所有
 * 未经授权，任何人不得复制、泄露、转载、使用，否则将视为侵权
 */

import * as React from "react";
import { RouteComponentProps } from "react-router-dom";
import { parse } from "qs";
import { decode } from "jsonwebtoken";
import Cookies from "js-cookie";

interface IPathData {
  access_token: string;
  refresh_token: string;
}
function checkNullObj(obj: object) {
  return Object.keys(obj).length === 0;
}
export default class UniformInterface extends React.Component<
  RouteComponentProps
> {
  public componentDidMount() {
    let pathData: IPathData = { access_token: "", refresh_token: "" };
    if (
      this.props.location.search &&
      this.props.location.search.indexOf("?") !== -1
    ) {
      const stringData = this.props.location.search.split("?")[1];
      pathData = parse(stringData);
    }
    const data: any = decode((pathData as any).access_token || "") || {};

    if (data && !checkNullObj(data)) {
      // localStorage.setItem('user_authority', JSON.stringify(data.authorities));
      Cookies.set(
        "user_token",
        pathData.access_token ? `bearer ${pathData.access_token}` : ""
      );
      this.props.history.push("/");
    } else {
      this.props.history.push("/login");
    }
  }
  public render() {
    return <>loading...</>;
  }
}
